<?php
namespace API\Controller;
use Think\Controller;
/**
* 帖子业务逻辑
*/
class PostController extends PublicController
{
	/**
	 * 发帖
	 * @return [type] [description]
	 */
	public function ppost(){
		if(IS_POST){
           $date['ptext'] = I('post.ptext');
           $date['uid'] = I('post.uid');
		}
		elseif(IS_GET){
		   $date['ptext'] = I('get.ptext');
           $date['uid'] = I('get.uid');
		}
		if(!empty($date['ptext'])&&!empty($date['uid'])){
              $p = M('Post');
              if(false!=$p->add($date)){
               return PublicController::codedate(1,"success");
              }
              else{
              	return PublicController::codedate(0,"fail");
              }
		}else{
           return PublicController::codedate(403,"");
		}
	}
	/**
	 * 帖子评价表
	 * @return [type] [description]
	 */
	public function rpost(){
      if (IS_POST) {
         $date['rptext'] = I('post.rptext');
         $date['uid'] = I('post.uid');
         $date['pid'] = I('post.pid');
      }elseif (IS_GET) {
      	 $date['rptext'] = I('get.rptext');
         $date['uid'] =  I('get.rptext ');
         $date['pid'] =  I('get.pid');
      }
      if(!empty($date['rptext'])&&!empty($date['uid'])&&!empty($date['pid'])){
      	$r = M('Rpost');
      	if (false!=$r->add($date)) {
      		return PublicController::codedate(1,"success");
      	}else{
      		return PublicController::codedate(0,"fail");
      	}
      }else{
      	return PublicController::codedate(403,"");
      }
	}
	public function postlist(){
		if ($_SERVER['REQUEST_METHOD']=="POST") {
        $page = isset($_POST['page']) ? $_POST['page'] : 1;
        $pageSize = isset($_POST['pagesize']) ? $_POST['pagesize'] : 8;
        }else {
        $page = isset($_GET['page']) ? $_GET['page'] : 1;
        $pageSize = isset($_GET['pagesize']) ? $_GET['pagesize'] : 8;
        }
        $offset = ($page - 1) * $pageSize;
        $p = D('Post');
        $postlist = $p ->relation(true)->order('pid desc') -> limit($offset ." , ".$pageSize)->select();
        foreach ($postlist as  $val) {
        	$r = M('Rpost');
        	$val['reply'] = $r -> where('pid = '.$val['pid']) -> count();
        	$temp[] = $val;
        }
         $sentdate = array('code'=>200,"postlist"=>$temp);
         return PublicController::yuejian($sentdate);
	}
	/**
	 * 帖子详情
	 * @return [type] [description]
	 */
	public function postsimp(){
		if (IS_POST) {
			$pid = I('post.pid');
		}
		elseif (IS_GET) {
			$pid =  I('get.pid');
		}
		if (!empty($pid)) {
			 $p = D('Post');
			 $simp = $p->relation(true)->where('pid = '.$pid)->find();
			 $r = D('Rpost');
			 $simp['replay'] = $r ->relation(true)-> where('pid = '.$pid) -> select();
			 $sentdate = array('code'=>200,"simppost"=>$simp);
              return PublicController::yuejian($sentdate);
		}else{
			return PublicController::codedate(403,"");
		}
	}
}